package com.mimosa.common.system.user;

import javax.mail.MessagingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mimosa.common.dao.HibernateDao;
import com.mimosa.common.service.CommonService;
import com.mimosa.util.config.YesNoType;

@Transactional
@Service
public class UserResetPassService extends CommonService<TUserResetPass, String> {
	@Autowired
	private TUserResetPassDao userResetPassDao;

	@Override
	protected HibernateDao<TUserResetPass, String> getHibernateDao() {
		return this.userResetPassDao;
	}

	@Autowired
	private UserService userService;

	/**
	 * 设置重置密码信息
	 * 
	 * @param user
	 * @throws MessagingException
	 */
	public String resetUserPass(TUser user) {
		TUserResetPass entity = new TUserResetPass();
		entity.setUser(user);
		super.saveEntity(entity);
		return entity.getId();
	}

	public void updateUserPass(String id, String userPass) {
		TUserResetPass entity = this.userResetPassDao.get(id);
		entity.setProcessed(YesNoType.Y.ordinal());
		this.userResetPassDao.update(entity);
		// 更新用户密码
		this.userService.resetPassword(entity.getUser().getId(), userPass);
	}
}